// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   ReaderHelper.java

package include.excel_export;

import java.util.*;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class ReaderHelper
{

	private String file;
	private SAXBuilder builder;
	private Document doc;
	private Element root;

	public ReaderHelper(Document doc)
	{
		file = null;
		builder = null;
		this.doc = null;
		root = null;
		this.doc = doc;
		root = doc.getRootElement();
	}

	public Document getDocument()
	{
		return doc;
	}

	public Vector getTableNicks()
	{
		Vector result = new Vector();
		for (Iterator tables = getTables().iterator(); tables.hasNext(); result.addElement(getTableNick((Element)tables.next())));
		return result;
	}

	public Vector getTableNames()
	{
		Vector result = new Vector();
		for (Iterator tables = getTables().iterator(); tables.hasNext(); result.addElement(getTableName((Element)tables.next())));
		return result;
	}

	public HashMap getTableNameAndNicks()
	{
		HashMap result = new HashMap();
		Element table;
		for (Iterator tables = getTables().iterator(); tables.hasNext(); result.put(getTableName(table), getTableNick(table)))
			table = (Element)tables.next();

		return result;
	}

	public HashMap getColumnNameAndNicks(String tablenick)
	{
		if (getTable(tablenick) != null)
		{
			HashMap result = new HashMap();
			Element col;
			for (Iterator loop = getColumns(getTable(tablenick)).iterator(); loop.hasNext(); result.put(getName(col), getNick(col)))
				col = (Element)loop.next();

			return result;
		} else
		{
			return null;
		}
	}

	public Vector getColumnNames(String tablenick)
	{
		if (getTable(tablenick) != null)
		{
			Vector result = new Vector();
			Element col;
			for (Iterator loop = getColumns(getTable(tablenick)).iterator(); loop.hasNext(); result.addElement(getName(col)))
				col = (Element)loop.next();

			return result;
		} else
		{
			return null;
		}
	}

	public Vector getColumnNicks(String tablenick)
	{
		if (getTable(tablenick) != null)
		{
			Vector result = new Vector();
			Element col;
			for (Iterator loop = getColumns(getTable(tablenick)).iterator(); loop.hasNext(); result.addElement(getNick(col)))
				col = (Element)loop.next();

			return result;
		} else
		{
			return null;
		}
	}

	public String getColumnName(String tablenick, String columnnick)
	{
		Element table = getTable(tablenick);
		for (Iterator loop = getColumns(table).iterator(); loop.hasNext();)
		{
			Element col = (Element)loop.next();
			if (getNick(col).equals(columnnick))
				return getName(col);
		}

		return null;
	}

	public String getTableNameFormNick(String tablenick)
	{
		return getTableName(getTable(tablenick));
	}

	public Element getTable(String tablenick)
	{
		for (Iterator loop = getTables().iterator(); loop.hasNext();)
		{
			Element table = (Element)loop.next();
			if (getTableNick(table).equals(tablenick))
				return table;
		}

		return null;
	}

	private List getTables()
	{
		List list = root.getChild("tables").getChildren("table");
		return list;
	}

	private String getTableNick(Element table)
	{
		return table.getAttributeValue("nick");
	}

	private String getTableName(Element table)
	{
		return table.getAttributeValue("name");
	}

	private List getColumns(Element table)
	{
		List list = table.getChild("columns").getChildren("column");
		return list;
	}

	private int getColumnsSize(Element table)
	{
		return getColumns(table).size();
	}

	private String getNick(Element column)
	{
		return column.getAttributeValue("nick");
	}

	private String getName(Element column)
	{
		return column.getAttributeValue("name");
	}
}
